package com.easyJava.builder;




import com.easyJava.bean.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 *  这是一个基础类，用于生成基础文件
 */
public class BuilderBase {

    private static final Logger logger = LoggerFactory.getLogger(BuilderBase.class);

    /**
     * 生成基础文件
     */
    public static void execute(){

       /* String folderPath = Constants.PATH_UTIL;
        String fileName = Constants.DATEUTILS;
        List<String> headerInfoList = new ArrayList<>();
        headerInfoList.add(Constants.PACKAGE + Constants.PACKAGE_UTIL + ";");
        WriteBaseFile(folderPath,fileName,headerInfoList);*/
        List<String> headerInfoList = new ArrayList<>();
        build(Constants.PATH_UTIL,Constants.DATEUTILS,headerInfoList,Constants.PACKAGE_UTIL);
        build(Constants.PATH_ENUMS,Constants.DATETIMEPATTERNENUM,headerInfoList,Constants.PACKAGE_ENUMS);
        build(Constants.PATH_MAPPER,Constants.BASEMAPPER,headerInfoList,Constants.PACKAGE_MAPPER);
        build(Constants.PATH_QUERY,Constants);
    }

    public static void build(String folderPath,String fileName, List<String> headerInfoList,String Package_name){
        headerInfoList.add(Constants.PACKAGE + Package_name + ";");
        WriteBaseFile(folderPath,fileName,headerInfoList);
        headerInfoList.clear();
    }

    //生成基础文件
    public static void WriteBaseFile(String FilePath , String FileName,List<String> headerInfoList){
        File folder = new File(FilePath);
        if(!folder.exists()){
            folder.mkdirs();
        }
        File file = new File(folder,FileName + ".java");

        //输出流
        OutputStream os = null;
        OutputStreamWriter ow = null;
        BufferedWriter bw = null;

        //输入流
        InputStream is = null;
        InputStreamReader ir = null;
        BufferedReader br = null;


        //读取文件,写入文件
        try{
            os = new FileOutputStream(file);
            ow = new OutputStreamWriter(os,"utf-8");
            bw = new BufferedWriter(ow);

            String path = BuilderBase.class.getClassLoader().getResource("template/" + FileName + ".txt").getPath();
            is = new FileInputStream(path);
            ir = new InputStreamReader(is,"utf-8");
            br = new BufferedReader(ir);

            String lineInfo = null;

            for(String headInfo :  headerInfoList){
                bw.write(headInfo);
                bw.newLine();
            }
            bw.newLine();

            while((lineInfo=br.readLine())!=null){
                bw.write(lineInfo);
                bw.newLine();
            }
            bw.flush();
        }catch (Exception e) {
            logger.error("生成基础文件失败", e);
        }finally {
            IOFinalDo(os,ow,bw,is,ir,br);
        }
    }

    //关闭流
    public static void IOFinalDo(OutputStream os , OutputStreamWriter ow,BufferedWriter bw,
                                 InputStream is,InputStreamReader ir,BufferedReader br){
        try{
            if(br != null){
                br.close();
            }
            if(ir != null){
                ir.close();
            }
            if(is != null){
                is.close();
            }
            if(bw != null){
                bw.close();
            }
            if(ow != null){
                ow.close();
            }
            if(os != null){
                os.close();
            }
        }catch (Exception e){
            logger.error("关闭流失败",e);
        }
    }
}
